package com.hyt.it.ogt.kq.service.gov.model.excel;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.utils.NumberUtil;
import com.hyt.it.ogt.kq.common.gov.utils.StringUtil;
import com.hyt.it.ogt.kq.common.gov.utils.TextUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 方法
 * </p>
 *
 * @author wangrl
 * @since 2021-09-03
 */
@Data
@ExcelIgnoreUnannotated
public class ImportTaskCandidateSheet {


    private String id;

    private String taskId;

    @Deprecated
    private String province;

    @Deprecated
    private String city;

    @Deprecated
    private String district;

    @ExcelProperty(index = 0)
    private String name;

    @ExcelProperty(index = 1)
    private String idType;

    @ExcelProperty(index = 2)
    private String idNumber;

    @ExcelProperty(index = 3)
    private String subject;

    @ExcelProperty(index = 4)
    private String phone;

    @ExcelProperty(index = 5)
    private String email;

    @ExcelProperty(index = 6)
    private String birthday;

    @ExcelProperty(index = 7)
    private String sex;

    //@ExcelProperty(index = 8)
    //private String selfLevel;

    @ExcelProperty(index = 8)
    private String minAreaLevel;

    @ExcelProperty(index = 9)
    private String deptId;

    @ExcelProperty(index = 10)
    private String answerType;

    @ExcelProperty(index = 11)
    private String language;

    @ExcelProperty(index = 12)
    private String admissionNumber;

    @Deprecated
    private String roomCode;

    @Deprecated
    private String timeCode;

    /**
     * 参数验证
     */
    public void valid(Integer rowIndex) throws KqException {
        final String sexRegex = "[1-2]",
                idTypeRegex = "[0-2]",
                answerTypeRegex="[1-2]";
        //必填字段校验
        if (StrUtil.hasBlank(this.name, this.sex, this.idType, this.idNumber, this.subject, this.answerType, this.language)) {
            throw new ExcelAnalysisException("第" + rowIndex + "条数据"
                    + "必填项（姓名、性别、证件类型、证件号码、报考科目编号、作答方式、语种类型）不能为空");
        }
        //性别约束校验
        if (!String.valueOf(this.sex).matches(sexRegex)) {
            throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "存在与性别约束不符合项");
        }
        //证件类型约束校验
        if (!String.valueOf(this.idType).matches(idTypeRegex)) {
            throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "存在与证件类型约束不符合项");
        }
        //作答类型约束校验
        if (!String.valueOf(this.answerType).matches(answerTypeRegex)) {
            throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "存在与作答方式约束不符合项");
        }
        //作答方式约束校验
        final String language = "1";
        if (!language.equals(this.language)) {
            throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "存在与语种类型约束不符合项");
        }
        if (TextUtil.isNotNull(this.birthday)) {
            //验证日期格式
            String [] split = this.birthday.split("/");
            //日期分段长度
            final int birthdayBlockSize = 3;
            if(split.length == birthdayBlockSize){
                //年
                String year = split[0];
                //月
                String month = split[1].length() == 1?"0"+split[1]:split[1];
                //日
                String day = split[2].length() == 1?"0"+split[2]:split[2];
                this.birthday = String.format("%s-%s-%s",year,month,day);
            }
            if (TextUtil.isNotDate(this.birthday)) {
                throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "出生日期格式不正确");
            }
        }
        //校验手机号
        if (StringUtils.isNotBlank(this.phone)) {
            boolean validPhone = StringUtil.isMobile(this.phone);
            if (!validPhone) {
                //手机号格式不合法
                throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "手机号格式不合法");
            }
        }
        //校验邮箱
        if (StringUtils.isNotBlank(this.email)) {
            if (!Validator.isEmail(this.email)) {
                //邮箱格式不合法
                throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "邮箱格式不合法");
            }
        }
        //校验自定义考场编号
        if (this.roomCode != null) {
            if (!NumberUtil.isPositiveInteger(this.roomCode)) {
                //自定义考场编号必须为正整数
                throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "自定义考场编号必须为正整数");
            }
        }
        //校验自定义批次编号
        if (this.timeCode != null) {
            if (!NumberUtil.isPositiveInteger(this.timeCode)) {
                //自定义批次编号必须为正整数
                throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "自定义批次编号必须为正整数");
            }
        }
        //校验自定义准考证号
        if (this.admissionNumber != null) {
            if (StringUtils.isBlank(this.admissionNumber)) {
                //自定义准考证号不能为空白符
                throw new ExcelAnalysisException("第" + rowIndex + "条数据" + "自定义准考证号不能为空白符");
            }
        }
    }

}
